package com.viknix.threadpool.manager.client.global;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiStyle;

/**
 * @Author: Dongqi
 * @Date: 2021/10/23 16:26
 * @Version 1.0
 * @Description: DynamicThreadPoolBannerHandler
 */
@Data
@AllArgsConstructor
public class DynamicThreadPoolBannerHandler implements InitializingBean {

    private boolean isBanner = true;

    private final String DYNAMIC_THREAD_POOL = " :: ThreadPool Manager :: ";

    private final int STRAP_LINE_SIZE = 20;

    @Override
    public void afterPropertiesSet() {
        printBanner();
    }

    private void printBanner() {
        String banner = " /__  ___/ //   ) )       /|    //| |                                                        \n" +
                        "   / /    //___/ /       //|   // | |     ___       __      ___      ___      ___      __    \n" +
                        "  / /    / ____ /       // |  //  | |   //   ) ) //   ) ) //   ) ) //   ) ) //___) ) //  ) ) \n" +
                        " / /    //             //  | //   | |  //   / / //   / / //   / / ((___/ / //       //       \n" +
                        "/ /    //             //   |//    | | ((___( ( //   / / ((___( (   //__   ((____   //   ";

        if (isBanner) {
            String version = getVersion();
            version = (version != null) ? " (v" + version + ")" : "0.0.1";

            StringBuilder padding = new StringBuilder();
            while (padding.length() < STRAP_LINE_SIZE - (version.length() + DYNAMIC_THREAD_POOL.length())) {
                padding.append(" ");
            }

            System.out.println(AnsiOutput.toString(AnsiColor.BRIGHT_RED, banner, AnsiColor.BRIGHT_BLUE, DYNAMIC_THREAD_POOL, AnsiColor.BRIGHT_BLUE,
                    padding.toString(), AnsiStyle.FAINT, version, "\n"));
        }
    }

    public static String getVersion() {
        final Package pkg = DynamicThreadPoolBannerHandler.class.getPackage();
        return pkg != null ? pkg.getImplementationVersion() : "";
    }

}
